package com.degon.hsyl.imagepreview.adapter;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.degon.hsyl.imagepreview.R;
import com.degon.hsyl.imagepreview.ui.PhotoView;
import com.degon.hsyl.imagepreview.util.DownloadImageUtil;

import java.util.List;

public class ImageViewPagerAdapter extends PagerAdapter {

    private FragmentActivity activity;
    private Context context;
    private List<String> urls;

    public ImageViewPagerAdapter(List<String> urls, Context context, FragmentActivity activity) {
        this.context = context;
        this.urls = urls;
        this.activity = activity;
    }

    @Override
    public int getCount() {

        return urls.size() == 0 ? 0 : urls.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = getItemView(R.layout.image_gallery_root_view);
        PhotoView photoView = view.findViewById(R.id.photo_view);
        RelativeLayout defaultView = view.findViewById(R.id.default_view);
        photoView.enable();
        ProgressBar loading = view.findViewById(R.id.loading_bar);
        loading.setVisibility(View.VISIBLE);


        if (urls.get(position).endsWith("gif")) {
            Glide
                    .with(this.context)
                    .asGif()
                    .load(urls.get(position))
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(photoView);
            loading.setVisibility(View.GONE);
        } else {
            Glide
                    .with(this.context)
                    .asBitmap()
                    .load(urls.get(position))
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .addListener(new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                            Toast.makeText(context, "图片加载失败,请稍后重试", Toast.LENGTH_SHORT).show();
                            loading.setVisibility(View.GONE);
                            photoView.setVisibility(View.GONE);
                            defaultView.setVisibility(View.VISIBLE);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            loading.setVisibility(View.GONE);
                            return false;
                        }
                    })
                    .into(photoView);
        }
        container.addView(view);

        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.finish();
                activity.overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
            }
        });

        photoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                showBottomDialog(position);
                return false;
            }
        });

        defaultView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.finish();
                activity.overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
            }
        });

        return view;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    private View getItemView(int layoutId) {
        return LayoutInflater.from(this.context).inflate(layoutId, null, false);
    }

    /**
     * 底部弹窗的实现
     */
    private void showBottomDialog(int position) {
        final Dialog dialog = new Dialog(context, R.style.DialogTheme);
        View view = View.inflate(context, R.layout.bottom_dialog_layout, null);
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        //下载图片
        dialog.findViewById(R.id.save_photo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (urls.get(position).startsWith("http") || urls.get(position).startsWith("https")) {
                    DownloadImageUtil.DownloadImage(urls.get(position), context);
                    dialog.dismiss();
                } else {
                    Toast.makeText(context, "这本身就是你手机的图片", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }
        });

        dialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

    }
}